package com.sskj.fabi.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.FabiListBean;
import com.sskj.fabi.bean.RecordTradeBean;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.RecordPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class RecordFragment extends BaseFragment<RecordPresenter> {

    @BindView(R2.id.c2c_record_rcv)
    RecyclerView c2cRecordRcv;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BaseAdapter<RecordTradeBean> baseAdapter;
    private int PAGE_SIZE = 6;
    private int PAGE = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_record;
    }

    @Override
    protected RecordPresenter getPresenter() {
        return new RecordPresenter();
    }


    public static RecordFragment newInstance() {
        Bundle args = new Bundle();
        RecordFragment fragment = new RecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mPresenter.getRecordList(PAGE, PAGE_SIZE);
            } else {
                userData = null;
            }
        });
        userViewModel.update();
        c2cRecordRcv.setLayoutManager(new LinearLayoutManager(getActivity()));
        c2cRecordRcv.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setLastDraw(false)
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.fabi_backgroud))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 10)));

        baseAdapter = new BaseAdapter<RecordTradeBean>(R.layout.fabi_record_item, null, c2cRecordRcv) {
            @Override
            public void bind(ViewHolder holder, RecordTradeBean item) {
                holder.setText(R.id.tvOrderNum, "订单号: " + item.getOrder_num());
                holder.setText(R.id.tvStatus, 1 == item.getStatus() ? "待付款" : 2 == item.getStatus() ? "已付款" : 3 == item.getStatus() ? "已完成" : 4 == item.getStatus() ? "申诉中" : 5 == item.getStatus() ? "已取消" : "冻结中");
                holder.setText(R.id.tvTitleFirstLeft, !SPUtil.get(SPConfig.ACCOUNT, "").equals(item.getChu_account()) ? TextUtils.isEmpty(item.getName1()) ? "**" : item.getName1().substring(0, 1) + "**" : TextUtils.isEmpty(item.getName2()) ? "**" : item.getName2().substring(0, 1) + "**");
                holder.setText(R.id.tvTitleFirstRight, NumberUtil.keep2(item.getTotal_price()) + " CNY");
                holder.setText(R.id.tvContentSecondLeft, (!SPUtil.get(SPConfig.ACCOUNT, "").equals(item.getChu_account()) ? "购买" : "出售"));
                holder.setTextColor(R.id.tvContentSecondLeft, getResources().getColor((!SPUtil.get(SPConfig.ACCOUNT, "").equals(item.getChu_account()) ? R.color.fabi_market_green : R.color.fabi_market_red)));
                holder.setText(R.id.tvTitleSecondRight, 1 == item.getType() ? "购买价" : "出售价");
                holder.setText(R.id.tvContentSecondRight, "¥ " + NumberUtil.keep2(item.getPrice()));
                holder.setText(R.id.tvContentThirdLeft, "¥ " + NumberUtil.keepMax2(item.getMin_price()) + " - ¥ " + NumberUtil.keepMax2(item.getMax_price()));
                holder.setText(R.id.tvContentThirdRight, NumberUtil.keep4(item.getTotal_num()));
                ClickUtil.click(holder.getView(R.id.ll_appeal_order), () -> {
                    ARouter.getInstance().build(RConfig.FABI_ORDER_DETAIL).withString("orderNum", item.getOrder_num()).navigation();
                });
            }
        };
        baseAdapter.setEmptyView();
        smartRefreshView.setOnRefreshListener(refreshLayout1 -> {  // 下拉刷新
            smartRefreshView.finishRefresh();
            PAGE = 1;
            mPresenter.getRecordList(PAGE, PAGE_SIZE);
        });
        smartRefreshView.setOnLoadMoreListener(new OnLoadMoreListener() {  // 上拉加载
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                smartRefreshView.finishLoadMore();
                PAGE++;
                mPresenter.getRecordList(PAGE, PAGE_SIZE);
            }
        });

    }

    public void setData(PageBean<RecordTradeBean> httpData) {
        if (PAGE == 1) {
            baseAdapter.setNewData(httpData.getList());
        } else {
            baseAdapter.addData(httpData.getList());
        }
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (userData != null && !isVisibleToUser && mPresenter != null) {
            PAGE = 1;
            mPresenter.getRecordList(PAGE, PAGE_SIZE);
        }
    }

}
